package com.net;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.*;

/**
 * Created by peak on 2016/1/11.
 */
public class URLConnectionTest {
    public static void main(String[] args)throws Exception{
//        CookieManager manager = new CookieManager();
//        CookieHandler.setDefault(manager);
//        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
//        CookieStore store = manager.getCookieStore();

        URL url = new URL("http://user.qzone.qq.com/p/r/cgi-bin/right_frame.cgi?uin=1416660239&param=3_1416660239_0%7C14_1416660239%7C8_8_1416660239_0_1_0_0_1%7C10%7C11%7C12%7C13_0%7C17%7C20%7C9_0_8_1%7C18&g_tk=58747165");
        URLConnection conn = url.openConnection();
//        conn.setRequestProperty("Cookie","verifysession=h01d69b483dee334ca4b718e0ad855e9275c0a85d5602a52d352d10e150985e39a9294addb9c3dc78f71eab1a7d790629c4; __Q_w_s_hat_seed=1; randomSeed=214932; qm_username=1416660239; qm_sid=027d72c2dad8a602cc129eed10eb16ba,qYWdITlhWMmR6T0tyLXVwYmNZTDVwQzVpbXdHSDVOcGVZTVA1cG1ZKmRPZ18.; pgv_pvi=7017195520; pgv_si=s6803466240; RK=hq1DQ23ZEC; __Q_w_s__QZN_TodoMsgCnt=1; qz_gdt=yydzgvreaaajktzocxqq; pgv_info=ssid=s2246207910; pgv_pvid=7928434608; o_cookie=1416660239; zzpaneluin=; zzpanelkey=; pt_clientip=2ce80a8204beae45; pt_serverip=4f550af17164d4f5; ptui_loginuin=1416660239; pt2gguin=o1416660239; uin=o1416660239; skey=@5OpFPArml; ptisp=ctc; qzone_check=1416660239_1452496199; ptcz=a3672c09bf43651d1536f9773a8969c08a4d363383713daaab2beccc5a2e4b42; p_skey=2Iw2i7TqfwUqDrf0DKMCaH8Z5uooVHbDHcC1CQhI4o4_; p_uin=o1416660239; pt4_token=toMzbMiXmYuUPAqUhjw4kQYiK68r63JVabYSVjdA0JQ_; qz_screen=1920x1200; QZ_FE_WEBP_SUPPORT=1; cpu_performance_v8=7; blabla=dynamic; Loading=Yes; qzspeedup=sdch");

        System.out.println(conn.getFileNameMap().getContentTypeFor("/a.jpg"));

//        BASE64Encoder encoder = new BASE64Encoder();
//        conn.setRequestProperty("Authorization","Basic "+encoder.encode("xx:xx".getBytes("utf-8")));
/*        Map<String,List<String>> headers = conn.getHeaderFields();
        for(String key:headers.keySet()){
            System.out.print(key + " :");
            List<String> list =headers.get(key);
            Iterator<String> it = list.iterator();
            while(it.hasNext()){
                System.out.println(it.next());
            }
        }*/

        try{
//            InputStream in = conn.getInputStream();
//            printStream(in,false);
        }catch (Exception e){
          //  printStream(conn.getErrorStream(),true);
        }


    }
    public static void printStream(InputStream in,boolean flag) throws Exception{
        InputStreamReader reader = new InputStreamReader(in,"utf-8");
        int c=-1;
        while((c=reader.read())!=-1){
            if(flag){
                System.err.println();
            }else{
                System.out.print((char)c);
            }
        }
    }
}
